home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / Utilities / MeasureMTF.c next >
Encoding:
C/C++ Source or Header  |  1993-03-04  |  11.0 KB  |  352 lines  |  [TEXT/KAHL]

  1. /*
  2. MeasureMTF.c
  3. Copyright © 1990-1993 Denis G. Pelli
  4. Measures the modulation transfer function of a monitor. Uses drifting gratings,
  5. both horizontal and vertical, of constant temporal frequency. The results
  6. are saved in a KaleidaGraph text file, MTF?.data, where ? stands for the screen
  7. number. The data file includes the dc and second harmonic.
  8. HISTORY:
  9. 10/11/90    dgp    wrote it.
  10. 10/12/90    dgp    added dc and harmonics.
  11. 3/18/91        dgp    updated to work with new PlotXY, but not tested
  12. 4/15/91        dgp Check for NewPaletteManager().
  13. 8/24/91        dgp    Made compatible with THINK C 5.0.
  14. 3/10/92    dgp    include mc68881.h
  15. 8/27/92    dgp    replace SysEnvirons() by Gestalt()
  16. 10/23/92 dgp try to read latest LuminanceRecord
  17. 2/7/93    dgp    updated to use SetPixelsQuickly.
  18. */
  19. #include "VideoToolbox.h"
  20. #include <math.h>
  21. #include "Luminance.h"
  22. #include <Packages.h>
  23. #if THINK_C
  24.     #include <profile.h>    /* for timing */
  25.     #include <console.h>
  26.     #define PROFILE 1
  27. #endif
  28. #define TWO_PI (2.0*PI)
  29. #define MAX(a,b) ((a)>(b)?(a):(b))
  30. #define MIN(a,b) ((a)<(b)?(a):(b))
  31.  
  32. void MeasureMTF(void);
  33. double MeasureContrast(GDHandle device,luminanceRecord *LP,int tPeriod,double c[10],WindowPtr plotWindow);
  34. double saw(double x);
  35.  
  36. void main(void)
  37. {
  38.     Require(gestalt8BitQD);
  39.     MeasureMTF();
  40. }
  41.  
  42. void MeasureMTF(void)
  43. {
  44.     GDHandle device,oldGDHandle;
  45.     CWindowPtr window;
  46.     WindowPtr plotWindow,mtfWindow;
  47.     static luminanceRecord LR;
  48.     OSErr error;
  49.     static char string[100],string2[100],outName[100];
  50.     unsigned long seconds;
  51.     FILE *outfile[2];
  52.     int i,tPeriod,width;
  53.     Rect r,srcRect,dstRect;
  54.     double a,b,f,fNominal,c,L,cOut,cH[10],cV[10];
  55.     PlotXYStyle mtfStyle[2];
  56.     static unsigned long row[1048];    // hopefully long enough for any video device
  57.     short oldScreen;
  58.  
  59.     #if THINK_C
  60.         console_options.ncols=100;
  61.         MaximizeConsoleHeight();
  62.         printf("\n");                       /* Initialize QuickDraw */
  63.     #else
  64.         InitGraf(&qd.thePort);
  65.         InitFonts();
  66.         InitWindows();
  67.         InitCursor();
  68.     #endif
  69.     #if PROFILE
  70.         InitProfile(200,1);
  71.         _profile=0;    /* disable profiling for the moment */
  72.     #endif
  73.  
  74.     printf("Welcome to MeasureMTF.\n");
  75.     #include "LuminanceRecord1.h"                    // read at compile time
  76.     oldScreen=LR.screen;
  77.     if(GetScreenDevice(1)!=NULL){
  78.         printf("Which screen would you like to calibrate (%d):",LR.screen);
  79.         gets(string);
  80.         sscanf(string,"%d",&LR.screen);
  81.     }
  82.     else LR.screen=0;
  83.     sprintf(string,"LuminanceRecord%d.h",LR.screen);
  84.     i=ReadLuminanceRecord(string,&LR,0);        // try to read latest luminanceRecord
  85.     if(i<1)printf("Warning: couldn't find “%s”. Calibrating screen %d.\n"
  86.         ,string,LR.screen);
  87.     else oldScreen=LR.screen;
  88.  
  89.     mtfStyle[0].continuing=0;
  90.     mtfStyle[0].lineWidth=1;
  91.     mtfStyle[0].symbolWidth=0;
  92.     mtfStyle[0].dash[0]=0;
  93.     mtfStyle[0].dashOffset=0;
  94.     mtfStyle[0].color=blackColor;
  95.     mtfStyle[1]=mtfStyle[0];
  96.     mtfStyle[1].color=blueColor;
  97.     
  98.     GetDateTime(&seconds);
  99.     sprintf(outName,"MTF%d.%s",LR.screen,DateString(seconds));
  100.     outfile[0]=stdout;
  101.     outfile[1]=fopen(outName,"w");
  102.     if(outfile[1]==NULL) PrintfExit("Sorry, can't create \"%s\".\007\n",outName);
  103.     SetFileInfo(outName,'TEXT','QKPT');    /* for Kaleidagraph */
  104.  
  105.     /* Find device corresponding to the experimental screen. */
  106.     oldGDHandle=GetGDevice();
  107.     device = GetScreenDevice(LR.screen);
  108.     if(NewPaletteManager())
  109.         SetDepth(device,8,1,1);    /* 8-bit pixels, color mode */
  110.     window = GDOpenWindow(device);
  111.     
  112.     if(oldScreen==LR.screen){
  113.         printf("Luminance was calibrated on %s\n",LR.date);
  114.         printf("at %.2f %s by %s\n",LR.LBackground,LR.units,LR.notes);
  115.         printf("Range is %.1f to %.1f %s.\n",LR.LMin,LR.LMax,LR.units);
  116.     }
  117.     L=LR.LBackground;    /* desired mean luminance */
  118.     printf("Enter desired display luminance in cd/m^2 (%.2f):",L);
  119.     fgets(string,sizeof(string)-1,stdin);
  120.     sscanf(string,"%lf", &L);
  121.  
  122.     c=0.9;
  123.     c=MIN(c,MIN((LR.LMax-L)/L,(L-LR.LMin)/L));
  124.     printf("Contrast %.1f\n",c);
  125.     printf("Computing sinusoidal lookup table . . .\n");
  126.     for(i=0;i<256;i++){
  127.         SetLuminance(device,&LR,i,L*(1.0+c*sin(TWO_PI*i/256.0)),L*(1.0-c),L*(1.0+c));
  128.     }
  129.  
  130.     tPeriod=64;                                    /* frames in one temporal period */
  131.     width=window->portRect.right;                /* pixels across screen */
  132.     SetRect(&r,0,0,2*tPeriod,2*tPeriod);
  133.     OffsetRect(&r,64,64);
  134.     plotWindow=NewWindow(NULL,&r,"\pL",1,noGrowDocProc,(WindowPtr) -1L,FALSE,0L);
  135.     SetRect(&r,0,0,192,192);
  136.     OffsetRect(&r,640-r.right-64,64);
  137.     mtfWindow=NewWindow(NULL,&r,"\pMTF",1,noGrowDocProc,(WindowPtr) -1L,FALSE,0L);
  138.     MeasureContrast(device,&LR,tPeriod,cH,plotWindow);        /* measure vBlack */
  139.     ffprintf(outfile,
  140.         "notes" "\tcycles/pixel" "\tcycles/screen" 
  141.         "\tHoriz. Grating Gain" "\tVert. Grating Gain" "\tVert./Horiz.");
  142.     fprintf(outfile[0],"\n");
  143.     fprintf(outfile[1],"\tc" "\tcH[0]" "\tcH[1]" "\tcH[2]" "\tcV[0]" "\tcV[1]" "\tcV[2]" 
  144.         "\t(cV[0]-cH[0])/cH[0]" "\tcV[2]/(c*V/H)^2" "\t(cV[0]-cH[0])/cH[0]/(c*V/H)^2"
  145.         "\n");
  146.     ffprintf(outfile,"notes");    /* put some text in notes column */
  147.     for(fNominal=0.0;;fNominal=MAX(fNominal*pow(2.0,1.0/2.0),fNominal+1.0/width)){
  148.         f=floor(fNominal*width+0.5)/width; /* integral periods for zero mean over line */
  149.         if(fNominal>0.5)f=0.5;
  150.         /* horizontal grating */
  151.         srcRect=dstRect=window->portRect;
  152.         dstRect.left=srcRect.right=1;
  153.         for(i=0;i<srcRect.bottom;i++){
  154.             row[0]=128.0+127.5*saw(TWO_PI*f*i);
  155.             SetWindowPixelsQuickly((WindowPtr)window,0,i,row,1);
  156.         }
  157.         /* CopyBits uses the inverse color table of the current GDevice. */
  158.         SetGDevice(device);
  159.         CopyBits((BitMap *)*window->portPixMap,(BitMap *)*window->portPixMap,
  160.             &srcRect,&dstRect,srcCopy,NULL);
  161.         SetGDevice(oldGDHandle);
  162.         cOut=MeasureContrast(device,&LR,tPeriod,cH,plotWindow);
  163.         ffprintf(outfile,"\t%10.5f" "\t%10.2f" "\t%12.6f",f,f*dstRect.right,cOut/c);
  164.         /* draw the MTF */
  165.         PlotXY(mtfWindow
  166.             ,log(f/(1./640.))/log(0.5/(1./640.)), log(cOut/c/0.3)/log(1.1/0.3)
  167.             ,&mtfStyle[0]);
  168.  
  169.         /* vertical grating */
  170.         srcRect=dstRect=window->portRect;
  171.         dstRect.bottom=srcRect.top=srcRect.bottom-1;
  172.         for(i=0;i<srcRect.right;i++)row[i]=128.0+127.5*saw(TWO_PI*f*i);
  173.         SetWindowPixelsQuickly((WindowPtr)window,0,srcRect.top,row,srcRect.right);
  174.         SetGDevice(device);
  175.         CopyBits((BitMap *)*window->portPixMap,(BitMap *)*window->portPixMap,
  176.             &srcRect,&dstRect,srcCopy,NULL);
  177.         SetGDevice(oldGDHandle);
  178.         cOut=MeasureContrast(device,&LR,tPeriod,cV,plotWindow);
  179.         ffprintf(outfile,"\t%20.6f" "\t%18.6f",cOut/c,cV[1]/cH[1]);
  180.         fprintf(outfile[0],"\n");
  181.         fprintf(outfile[1],"\t%f" "\t%f" "\t%f" "\t%f" "\t%f" "\t%f" "\t%f"
  182.             ,c,cH[0],cH[1],cH[2],cV[0],cV[1],cV[2]);
  183.         a=(cV[0]-cH[0])/cH[0];    /* dc error, expressed as a contrast */
  184.         b=cV[1]/cH[1];            /* contrast gain of video amplifier */
  185.         fprintf(outfile[1],"\t%f" "\t%f" "\t%f" "\n"
  186.             ,a,cV[2]/(c*b*c*b),a/(c*b*c*b));
  187.         /* draw the MTF */
  188.         PlotXY(mtfWindow
  189.             ,log(f/(1./640.))/log(0.5/(1./640.)), log(cOut/c/0.3)/log(1.1/0.3)
  190.             ,&mtfStyle[1]);
  191.         
  192.         if(f==0.5)break;
  193.     }
  194.     
  195.     /* print notes */
  196.     IUDateString(seconds,longDate,(unsigned char *)string);
  197.     PtoCstr((unsigned char *)string);
  198.     IUTimeString(seconds,FALSE,(unsigned char *)string2);
  199.     PtoCstr((unsigned char *)string2);
  200.     ffprintf(outfile,"%s %s\n",string2,string);
  201.     ffprintf(outfile,"%.1f Hz\n",1.0/(tPeriod*0.015));
  202.     ffprintf(outfile,"%.2f cd/m^2\n",L);
  203.     ffprintf(outfile,"%.3f contrast\n",c);
  204.     ffprintf(outfile,"\n" "screen %d\n",LR.screen);
  205.     ffprintf(outfile,"\"%s\" monitor %s\n",LR.name,LR.id);
  206.     ffprintf(outfile,"on %s\n",LR.date);
  207.     ffprintf(outfile,"by %s\n",LR.notes);
  208.     ffprintf(outfile,"at %.2f %s\n",LR.LBackground,LR.units);
  209.  
  210.     GDDisposeWindow(device,window);
  211.     if(outfile[1] != NULL)fclose(outfile[1]);
  212.     printf("The sum of the times for GetVoltage() and LoadLuminances() should\n"
  213.         "be about one frame, 15 ms. If it's longer, e.g. 30 ms, you should reduce\n"
  214.         "the value of \"frames\" in MeasureContrast().\n");
  215.     DisposeWindow(plotWindow);
  216.     DisposeWindow(mtfWindow);
  217. }
  218.  
  219.  
  220. double MeasureContrast(GDHandle device,luminanceRecord *LP
  221.     ,int tPeriod,double c[10],WindowPtr plotWindow)
  222. /*
  223. Measures the contrast of a drifting grating of unknown phase.
  224. The temporal period is tPeriod frames. The c[] array is
  225. filled with the amplitude spectrum, where c[i] is the contrast of the i-th harmonic,
  226. except that c[0], which would always be 1, instead is set to the dc level.
  227. */
  228. {
  229.     double v[256];
  230.     double p[10];
  231.     register int i,j;
  232.     static ColorSpec doubleTable[512];
  233.     static double sinTable[256],cosTable[256];
  234.     static int tablePeriod=0;
  235.     register double a,b;
  236.     double frequency=2000.,gain=100.;
  237.     double frames=0.6;    /* How long the A/D should spend sampling the photometer */
  238.     long n;
  239.     static double vBlack=0.0;
  240.     static int blackSet=0;
  241.     char string[80];
  242.     WindowPtr oldPort;
  243.     int v0,ip;
  244.  
  245.     if(tPeriod>256){
  246.         printf("Warning. Reducing tPeriod to 256.\n");
  247.         tPeriod=256;
  248.     }
  249.     if(tablePeriod != tPeriod){
  250.         for(i=0;i<tPeriod;i++){
  251.             sinTable[i]=sin(i*TWO_PI/tPeriod);
  252.             cosTable[i]=cos(i*TWO_PI/tPeriod);
  253.         }
  254.         tablePeriod=tPeriod;
  255.     }
  256.     
  257.     n=(long)floor(0.5+frequency*0.015*frames);
  258.  
  259.     RemeasureContrast:
  260.     if(!blackSet){
  261.         doubleTable[0].rgb.red=doubleTable[0].rgb.green=doubleTable[0].rgb.blue=0;
  262.         for(i=0;i<256;i++)doubleTable[i]=doubleTable[i+256]=doubleTable[0];
  263.         LoadLuminances(device
  264.             ,(luminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  265.         printf("Please block all light to set black. Hit cr when ready:");
  266.         gets(string);
  267.         vBlack=0.0;
  268.     }
  269.     else for(i=0;i<256;i++)doubleTable[i]=doubleTable[i+256]=LP->table[i];
  270.  
  271.     /* warm up for one period before collecting data */
  272.     SetPriority(7);
  273.     for(i=0;i<tPeriod;i++){
  274.         LoadLuminances(device
  275.             ,(luminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  276.         GetVoltage(1,&gain,&frequency,n,NULL);
  277.         v[i]=0.0;
  278.     }
  279.     _profile=1;
  280.     for(i=0;i<tPeriod*4;i++){
  281.         LoadLuminances(device
  282.             ,(luminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  283.         v[i%tPeriod]+=GetVoltage(1,&gain,&frequency,n,NULL);
  284.     }
  285.     _profile=0;
  286.     SetPriority(0);
  287.     a=0.0;
  288.     for(i=0;i<tPeriod;i++) a+=v[i];
  289.     c[0]=a/tPeriod-vBlack;
  290.     if(!blackSet){
  291.         vBlack=c[0];
  292.         blackSet=1;
  293.         printf("Black %g mV\n",vBlack*1000.0);
  294.         printf("Now please remove light block. Hit cr when ready:");
  295.         gets(string);
  296.         goto RemeasureContrast;
  297.     }
  298.     for(j=1;j<10;j++){
  299.         a=b=0.0;
  300.         for(i=0;i<tPeriod;i++){
  301.             a+=sinTable[i*j%tPeriod]*v[i];
  302.             b+=cosTable[i*j%tPeriod]*v[i];
  303.         }
  304.         c[j]=2.0*sqrt(a*a+b*b)/tPeriod/c[0];
  305.         p[j]=atan2(a,b);
  306.     }
  307.     /* Show one period of raw data, fundamental, raw minus fundamental, 2nd harmonic */
  308.     GetPort(&oldPort);
  309.     SetPort(plotWindow);
  310.     BringToFront(plotWindow);
  311.     EraseRect(&plotWindow->portRect);
  312.     v0=plotWindow->portRect.bottom/4;
  313.     SetOrigin(0,-2*v0);
  314.     MoveTo(0,-v0*(v[0]-vBlack)/c[0]);
  315.     for(i=1;i<tPeriod;i++)LineTo(i,-v0*(v[i]-vBlack)/c[0]);
  316.     SetOrigin(0,-4*v0);
  317.     j=1;
  318.     ip=tPeriod*(1.0-p[j]/TWO_PI);
  319.     a=-v0*(0.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  320.     MoveTo(0,-v0*(v[0]-vBlack)/c[0]-a);
  321.     for(i=1;i<tPeriod;i++){
  322.         a=-v0*(0.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  323.         LineTo(i,-v0*(v[i]-vBlack)/c[0]-a);
  324.     }
  325.     ForeColor(blueColor);
  326.     for(j=1;j<3;j++){
  327.         SetOrigin(-tPeriod,-2*v0*j);
  328.         ip=tPeriod*(1.0-p[j]/TWO_PI);
  329.         i=0;
  330.         a=-v0*(1.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  331.         MoveTo(0,a);
  332.         for(i=1;i<tPeriod;i++){
  333.             a=-v0*(1.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  334.             LineTo(i,a);
  335.         }
  336.     }
  337.     ForeColor(blackColor);
  338.     SetOrigin(0,0);
  339.     SetPort(oldPort);
  340.     return c[1];
  341. }
  342.  
  343.  
  344. double saw(double x)
  345. /* returns sawtooth function with same phase, amplitude, and symmetry as sin() */
  346. {
  347.     x/=TWO_PI;
  348.     x-=0.5;
  349.     x-=floor(x);
  350.     return 2.0*x-1.0;
  351. }
  352.